package KK.Hunter.MapEdit.Logic
{ 
	
	import KK.EMVC.View.View;
	
	import flash.display.Graphics;
	import flash.geom.Point;
	
	public class MapGrid extends View
	{
		
		private var _tileW:Number;//高度格子像素
		private var _tileH:Number;//高度格子像素
		private var _xCount:int;//x向
		private var _yCount:int;//y
		
		public function MapGrid()
		{
		}
		
		/**
		 * 画菱形边框网格(整体也为菱形)
		 * @param uCount  U轴向网格个数
		 * @param vCount  V轴向网格个数
		 * @param _tileW  网格宽
		 * @param _tileH  网格高
		 * @param _lineColor 网格颜色
		 * 
		 */
		public function drawDiamond(graphics:Graphics,_xCount:int, _yCount:int,_tileW:Number,_tileH:Number,_lineColor:uint = 0xff1212):void
		{
			
			this._tileH=_tileH;
			this._tileW=_tileW;
			this._xCount=_xCount;
			this._yCount=_yCount;
			
			
			graphics.lineStyle(1, _lineColor, 1);
			//U向坐标
			var u:int;
			//V向坐标
			var v:int;
			//线起始点像素坐标
			var originPos:Point;
			//线途经点像素坐标
			var lineToPos:Point;
			
			//沿V向画线
			while(u <= _yCount)
			{
				v = 0;
				originPos = getPixelPoint(u,v);
				graphics.moveTo(originPos.x,originPos.y);
				
				while(v < _xCount)
				{
					++v;
					lineToPos = getPixelPoint(u,v);
					graphics.lineTo(lineToPos.x,lineToPos.y);
				}
				++u;
			}
			
			u = 0;
			v = 0;
			
			//沿U向画线
			while(v <= _xCount)
			{
				u = 0;
				originPos = getPixelPoint(u,v);
				graphics.moveTo(originPos.x,originPos.y);
				
				while(u < _yCount)
				{
					++u;
					lineToPos = getPixelPoint(u,v);
					graphics.lineTo(lineToPos.x,lineToPos.y);
				}
				++v;
			}
		}
		
		/**
		 * 获取UV点坐标
		 * @param _x x坐标
		 * @param _y y坐标
		 * @param _tw 网格宽
		 * @param _th 网格高
		 * @param _count U向网格数量
		 * @return 
		 * 
		 */
		public function TouchGrid(_x:Number,_y:Number):Point
		{
			var org_x:Number = 0;
			var org_y:Number = this._yCount * this._tileH/2;
			
			var _v:int = Math.floor((_x - org_x)/this._tileW - (_y - org_y)/this._tileH);
			var _u:int = Math.floor((_x - org_x)/this._tileW + (_y - org_y)/this._tileH);
			
			return new Point(_u,_v);
		}
		
		/**
		 * 获取像素点坐标
		 * @param _u U向坐标
		 * @param _v v向坐标
		 * @param _tw 网格宽
		 * @param _th 网格高
		 * @param _count 网格数量
		 * @return 像素点x,y坐标
		 */
		public function getPixelPoint(x:int,y:int):Point
		{
			var org_x:Number = 0;
			var org_y:Number = this._yCount * this._tileH/2;
			var _x:Number = org_x + this._tileW/2 * (y + x);
			var _y:Number = org_y + this._tileH/2 * (x - y);
			return new Point(_x,_y);
		}
	}//end class
}